package com.infore.exchange.main.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.exchange.TJobInfo;
import com.infore.common.model.entity.exchange.TTaskInfo;
import com.infore.common.model.entity.vo.exchange.TTaskInfoVo;
import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiListResponse;
import com.infore.common.response.ApiResponse;
import com.infore.common.utils.StringUtils;
import com.infore.exchange.main.quartz.SchedulingRunnable;
import com.infore.exchange.main.service.TTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 任务信息表 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-02-13
 */
@RestController
@Api(value = "任务信息表前端控制器", tags = "任务信息表前端控制器")
@RequestMapping("/tTaskInfo")
public class TTaskInfoController extends BaseController {
	private String prefix = "/tTaskInfo";

	@Autowired
	private TTaskInfoService TTaskInfoService;

	@GetMapping()
	public String TTaskInfoController(){
		return prefix;
	}

	/**
	 * 查询任务信息表列表
	 * @param ttaskinfoVo 任务信息表的vo类
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-02-13
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "查询任务信息表列表", notes = "任务信息表列表查询 ")
	public Msg<Object> list(TTaskInfoVo ttaskinfoVo){
		//sql查询条件
		Wrapper<TTaskInfo> wrapper = null ;
		try {
			wrapper = (Wrapper<TTaskInfo>) WrapperHelp.addCondition(new EntityWrapper<TTaskInfo>(), ttaskinfoVo);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(500,e.getMessage());
		}
		//查询方法
		Page<TTaskInfo> eppPage = new EppPage<TTaskInfo>(TTaskInfoService,ttaskinfoVo,wrapper);
		return ResultUtil.successPage(TTaskInfoService.selectPage(eppPage, wrapper));
	}


	@ResponseBody
	@ApiOperation(value = "添加或编辑任务信息表", notes = "添加或编辑任务信息表")
	@RequestMapping(value="/addOrEdit",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public Msg<Object> addOrEdit(@RequestBody TTaskInfo ttaskinfo){
		boolean flag = false;
		if(StringUtils.isEmpty(ttaskinfo.getTaskId()) ){
			//新增
			Wrapper<TTaskInfo> wrapper1 = new  EntityWrapper<TTaskInfo>();
			wrapper1.eq("task_name", ttaskinfo.getTaskName());
			List<TTaskInfo> entityList = TTaskInfoService.selectList(wrapper1);
			if(entityList.size()>0) {
				return ResultUtil.error("任务名称已存在");
			}

			Wrapper<TTaskInfo> wrapper2 = new  EntityWrapper<TTaskInfo>();
			wrapper2.eq("transform_params", ttaskinfo.getTransformParams());
			entityList = TTaskInfoService.selectList(wrapper1);
			if(entityList.size()>0) {
				return ResultUtil.error("交换ID已绑定任务："+entityList.get(0).getTaskName());
			}
			ttaskinfo.setTaskState(1);
			ttaskinfo.setRunningState(0);
			ttaskinfo.setCreateTime(new Date());
			ttaskinfo.setUpdateTime(new Date());
			flag = TTaskInfoService.insert(ttaskinfo);
		}else{
			TTaskInfo targetTaskinfo = TTaskInfoService.selectById(ttaskinfo.getTaskId());
			CopyOptions copyOptions = new CopyOptions();
			copyOptions.setIgnoreNullValue(true);
			BeanUtil.copyProperties(ttaskinfo,targetTaskinfo,copyOptions);
			ttaskinfo.setUpdateTime(new Date());
			flag = TTaskInfoService.updateById(ttaskinfo);

		}
		return ResultUtil.setResult(flag,ttaskinfo.getTaskId());
	}


	/**
	 * 删除任务信息表
	 *
	 * @param  taskId
	 * @return Msg<Object>
	 * @author lijh
	 * @since 2023-02-13
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "删除任务信息表", notes = "删除任务信息表")
	public Msg<Object> del(@RequestParam(name = "taskId") String  taskId) {

		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("task_id", taskId);
		boolean flag = TTaskInfoService.deleteByMap(columnMap);
		return ResultUtil.setResult(flag);
	}

	@ResponseBody
	@ApiOperation(value = "修改任务状态", notes = "修改任务状态")
	@RequestMapping(value="/changesStatus",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ApiResponse changesStatus(@RequestParam(name = "taskId",required = true) String  taskId,
									 @RequestParam(name = "status",required = true) Integer status) {
		TTaskInfoService.updateTaskStateById(taskId, status);
		return ApiResponse.ok();
	}


	@RequestMapping(value = "/notBindTask", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "未绑定的任务", notes = "未绑定的任务")
	public ApiListResponse<TTaskInfo> notBindTask() {
		return ApiListResponse.ok(TTaskInfoService.notBindTask());
	}
}
